import {setUserInfo} from '@/redux/actions/index.js'
import store from '@/redux/store.js'
import dayjs from 'dayjs';
import quarterOfYear from 'dayjs/plugin/quarterOfYear';
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
import weekday from 'dayjs/plugin/weekday';
import updateLocale from 'dayjs/plugin/updateLocale'
dayjs.extend(updateLocale)
dayjs.extend(weekday)
dayjs.extend(quarterOfYear)
dayjs.extend(isSameOrBefore)
dayjs.extend(isSameOrAfter)
dayjs.updateLocale('en', {//设置周一为一周的开始
  weekStart: 1,
})

/*
获取同环比时间
type 同环比类型  1同比 2环比
nowTime 当前时间 格式：['2023-07-04','2023-07-04']
dateType 日期类型
time  若为环比则为结束时间，若为同比则为开始时间 格式：'2023-07-04'
*/
export const getTHBTime = (type,nowTime,dateType,time)=>{
    console.log(type,nowTime,dateType,time)
    const currentYear = dayjs().subtract(1, 'day').year()
    const currentQuarter = dayjs().subtract(1, 'day').quarter()
    const currentMonth = dayjs().subtract(1, 'day').month()
    const selectYear = dayjs(nowTime[1]).year()
    const selectQuarter = dayjs(nowTime[1]).quarter()
    const selectMonth = dayjs(nowTime[1]).month()
    if(currentYear === selectYear && currentQuarter === selectQuarter && currentMonth === selectMonth){
        //按天计算同环比
        let days = dayjs(nowTime[1]).diff(dayjs(nowTime[0]),'day')
        if(type == 1){
          return dayjs(time).add(days,'day').format('YYYY-MM-DD')
        }else{
          // if(dateType === 'month'){
          //   time = dayjs(time).endOf('month')
          // }else if(dateType === 'quarter'){
          //   time = dayjs(time).endOf('quarter')
          // }
          console.log('days',days)
          return dayjs(time).subtract(days,'day').format('YYYY-MM-DD')
        }
    }
    function getFullTime(diffNum,dateType){
        if(type == 1){
          return dayjs(time).add(diffNum,dateType).endOf(dateType).format('YYYY-MM-DD')
        }else{
          return dayjs(time).subtract(diffNum,dateType).startOf(dateType).format('YYYY-MM-DD')
        }
      }
      let nums = 0
      switch (dateType){
          case 'day':
              nums = dayjs(nowTime[1]).diff(dayjs(nowTime[0]),'day')
              return getFullTime(nums,'day')
          case 'week':
              nums = dayjs(nowTime[1]).diff(dayjs(nowTime[0]),'week')
              console.log(dayjs(nowTime[1]).format('YYYY-MM-DD'),dayjs(nowTime[0]).format('YYYY-MM-DD'),'nums',nums)
              return getFullTime(nums,'week')
          case 'month':
              nums = dayjs(nowTime[1]).diff(dayjs(nowTime[0]),'month')
              return getFullTime(nums,'month')
          case 'quarter':
              nums = dayjs(nowTime[1]).diff(dayjs(nowTime[0]),'quarter')
              return getFullTime(nums,'quarter')
          case 'year':
              nums = dayjs(nowTime[1]).diff(dayjs(nowTime[0]),'year')
              return  getFullTime(nums,'quarter')
      }
}
/*
判断是否为当期
date 需要进行判断的时间 格式：'2023-07-04'
type 时间类型 日周月季年 判断date是否在当前日周月季年内
*/
export const isCurrent = (date,type)=>{
  let nowDate = dayjs().subtract(1,'day')
  let start = dayjs(nowDate).startOf(type)
  let end = dayjs(nowDate).endOf(type)
  let isZQ = false
  isZQ = dayjs(date).isSameOrAfter(start) && dayjs(date).isSameOrBefore(end)
  return isZQ;
}

export const getUser = ()=>{
  let userInfo = JSON.parse(localStorage.getItem('userInfo'))
  store.dispath(setUserInfo(userInfo))
  return userInfo
}

